package com.woniuxy.joylifeshop.service.serviceImpl;





import com.woniuxy.common.constant.MarketShopConstant;
import com.woniuxy.common.entity.*;
import com.woniuxy.common.mapper.ShopCommodityImageMapper;
import com.woniuxy.common.mapper.ShopCommodityMapper;
import com.woniuxy.common.mapper.ShopCommodityTypeMapper;
import com.woniuxy.common.mapper.ShopMerchantMapper;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.CommentException;
import com.woniuxy.joylifeshop.exception.CommodityException;
import com.woniuxy.joylifeshop.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    ShopCommodityMapper shopCommodityMapper;

    @Autowired
    ShopCommodityImageMapper shopCommodityImageMapper;

    @Autowired
    ShopMerchantMapper shopMerchantMapper;

    @Autowired
    ShopCommodityTypeMapper shopCommodityTypeMapper;

    /**
    * @Author SooYoo
    * @Description 通过商品的id找到商品对象,图片对象,商家对象
    * @Param
    * @Return
    **/
    @Override
    public ObjectDto readCommodityById(String id) {

        try {
            ShopCommodity shopCommodity = shopCommodityMapper.selectByPrimaryKey(id);
            ShopCommodityImage shopCommodityImage = shopCommodityImageMapper.selectByPrimaryKey(shopCommodity.getImgId());
            ShopMerchant shopMerchant = shopMerchantMapper.selectByPrimaryKey(shopCommodity.getMerchantId());
            ShopCommodityType shopCommodityType =shopCommodityTypeMapper.selectByPrimaryKey(shopCommodity.getTypeId());

            ObjectDto objectDto = new ObjectDto();
            objectDto.setData(shopCommodity);
            objectDto.setData2(shopCommodityImage);
            objectDto.setData3(shopMerchant);
            objectDto.setData4(shopCommodityType);

            return objectDto;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommodityException();
        }
    }

    /**
    * @Author SooYoo
    * @Description 通过comid找到商品对象
    * @Param
    * @Return
    **/
    @Override
    public ShopCommodity readCommodityByIdSimple(String comId) {

        ShopCommodity shopCommodity = null;
        try {
            shopCommodity = shopCommodityMapper.selectByPrimaryKey(comId);

            return shopCommodity;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommodityException();
        }
    }

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public List<ShopCommodity> readCommoditysByMerchantId(String merchantId) {

        try {
            ShopCommodityExample shopCommodityExample = new ShopCommodityExample();
            ShopCommodityExample.Criteria criteria = shopCommodityExample.createCriteria();
            criteria.andMerchantIdEqualTo(merchantId);

            List<ShopCommodity> shopCommodities = shopCommodityMapper.selectByExample(shopCommodityExample);

            return  shopCommodities;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommentException();
        }


    }

    /**
    * @Author SooYoo
    * @Description 添加一个商品对象
    * @Param
    * @Return
    **/
    @Override
    public ShopCommodity addOneCommodity(ShopCommodity shopCommodity) {

        try {
            shopCommodity.setShopId(UUID.randomUUID().toString());
            shopCommodity.setCredential(new Byte(MarketShopConstant.CHECKING.toString()));
            shopCommodity.setSales(0L);
            shopCommodity.setStandard("001");

            shopCommodityMapper.insert(shopCommodity);

            return shopCommodity;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommentException();
        }

    }

    @Override
    public void addCommodityImgid(ShopCommodity shopCommodityAfter) {

        try {
            shopCommodityMapper.updateByPrimaryKey(shopCommodityAfter);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommodityException();
        }

    }

    @Override
    public void addSales(String comId, Long count) {


        ShopCommodity shopCommodity = shopCommodityMapper.selectByPrimaryKey(comId);

        shopCommodity.setSales(shopCommodity.getSales()+count);

        shopCommodityMapper.updateByPrimaryKey(shopCommodity);


    }


    /**
    * @Author SooYoo
    * @Description  通过检索找到商品的情况
    * @Param
    * @Return
    **/
    @Override
    public List<ObjectDto> readCommodityByName(String name) {

        try {
            //找到商品对象
            ShopCommodityExample shopCommodityExample = new ShopCommodityExample();
            ShopCommodityExample.Criteria criteria = shopCommodityExample.createCriteria();
            criteria.andNameLike("%"+name+"%");
            List<ShopCommodity> shopCommodities = shopCommodityMapper.selectByExample(shopCommodityExample);
            //找到图片对象
            List<ShopCommodityImage> shopCommodityImages = new ArrayList<>();
            for (ShopCommodity shopCommodity:shopCommodities){
                ShopCommodityImage shopCommodityImage = shopCommodityImageMapper.selectByPrimaryKey(shopCommodity.getImgId());
                shopCommodityImages.add(shopCommodityImage);
            }
            System.out.println(shopCommodityImages);
            //找到商家对象
            List<ShopMerchant> shopMerchants = new ArrayList<>();
            for (ShopCommodity shopCommodity:shopCommodities){
                ShopMerchant shopMerchant = shopMerchantMapper.selectByPrimaryKey(shopCommodity.getMerchantId());
                shopMerchants.add(shopMerchant);
            }
            //找到商品类别对象
            List<ShopCommodityType> shopCommodityTypes = new ArrayList<>();
            for (ShopCommodity shopCommodity:shopCommodities){
                ShopCommodityType shopCommodityType = shopCommodityTypeMapper.selectByPrimaryKey(shopCommodity.getTypeId());
                shopCommodityTypes.add(shopCommodityType);
            }
            //填充传输对象
            List<ObjectDto> objectDtos = new ArrayList<>();
            for(int i = 0 ; i<shopCommodities.size();i++){
                ObjectDto objectDto = new ObjectDto();
                objectDto.setData(shopCommodities.get(i));
                objectDto.setData2(shopCommodityImages.get(i));
                objectDto.setData3(shopMerchants.get(i));
                objectDto.setData4(shopCommodityTypes.get(i));
                objectDtos.add(objectDto);
            }
           System.out.println(objectDtos);
            return  objectDtos;
        } catch (Exception e) {
            e.printStackTrace();
            throw  new CommodityException();
        }
    }





}
